<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Cookie;


class Login extends Controller
{
    public function login_view(){
        $logout = input('get.logout');
        if ($logout == true){
            $this->logout();
        }
        return $this->fetch('login');
    }
    
    protected function logout()
    {
        if (Session::has('user_id','admin')){
            Session::delete('user_id','admin');
        }
        if (Session::has('user_name','admin')){
            Session::delete('user_name','admin');
        }
    }
    //登录页面
    public function login() {
        $result = array('err' => '1','content' => "");
        $data = input('post.');
        $rules = [
            'name'=>'require|max:20',
            'password'=>'require|max:255','密码必须'
        ];
        $message = [
            'name.require' => '用户名必须',
            'password.require' => '密码必须'
        ];
        $res = $this->validate($data, $rules,$message);
        if ($res !== true){
            $result['content'] = $res;
            return $result;
        }
        $data['password'] = md5($data['password']);
        $user_model = model('User');
        $user_id = $user_model->checkLoginUser($data);
        if ($user_id == false){
            $result['content'] = '用户或密码名错误！';
            $result['data'] = $data['password'];
            return $result;
        }
        Session::init([
            'prefix' => 'admin',
            'auto_star' => true,
        ]);
        Session::set('user_id',$user_id);
        Cookie::init([
            'prefix' => 'admin_', 
            'expire' => 7200,
            'setcookie' => true
        ]);
        Cookie::set('login','1');
        Cookie::set('login_time',time()+7200,['prefix' => 'admin_']);
        Session::set('DSL','DSL');
        $result['err'] = '0';
        $result['url'] = url('admin/main/main_view','','');
        return $result;   
    }
    
   
    public function check_login(){
        $result = array('err' => '1','content' => "");
        $if_outTime = $this->check_login_outTime();
        if ($if_outTime){
            $result['content'] = "登录超时,请重新登录";
            $result['url'] = url('admin/login/login_view','','');
            return $result;
        }
        $add_time = $this->add_login_time();
        if (!$add_time){
            $result['err'] = '-1';
            $result['content'] = '加时失败';
            return $result;
        }
        $result['content'] = '正常！';
        $result['err'] = '0';
        return $result;      
    }
    
    /*
     * return true=登录超时；false=登录未超时
     */
    protected function check_login_outTime(){
        $cookie_login_time = Cookie::has('login_time','admin_')?Cookie::get('login_time','admin_'):0;
        $now = time();
        $cookie_login = Cookie::has('login','admin_')?Cookie::get('login','admin_') : "";
        if (!$cookie_login || $cookie_login_time < $now){
            return true;
        }
        return false; 
    }
    
    protected function add_login_time(){
        $now = time();
        $cookie_login_time = Cookie::has('login_time','admin_')?Cookie::get('login_time','admin_'):$now;
        if ($cookie_login_time - $now < 1200){
            $cookie_login_time = $now + 3600;
            Cookie::set('login_time',$cookie_login_time,['prefix' => 'admin_']);
        }
        return true;
    }
    
}